<?php
namespace App\Http\Controllers\AppApi\Home;

use App\Services\Admin\SearchService;
use App\Services\Admin\UserService;
use Illuminate\Http\Request;

use App\Models\Admin\SearchModel;
use App\Models\Admin\SearchLogModel;

class SearchController extends BaseController
{
    /**
     * 搜索
     * 制作者：九哥
     */

    protected $service;
    protected $user;

    public function __construct()
    {
        parent::__construct();
        $this->service = new SearchService();
        $this->user = new UserService();
    }

    /**
     * 搜索历史
     */
    public function getHistoryList(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $resData = $this->service->getApiLogList($uid);
        if (!count($resData)) { return $this->toJson(-3); }
        return $this->toJson(0,$resData);
    }

    /**
     * 搜索结果
     */
    public function getIndexList(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $word = $request->word;
        if (!$token || !$word) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $resData = $this->service->getApiIndex($word,$type);//这儿拉下一个参数$type,zwp
        //设置用户搜索记录
        $logId = $this->service->setApiLog($uid,$word);
        if (!$resData) { return $this->toJson(-3); }
        return $this->toJson(0,$resData);
    }

//    /**
//     * 同步坐标
//     */
//    public function setPoint(Request $request)
//    {
//        if ($request->app_access!=$this->app_access) {
//            return $this->toJson(-1);
//        }
//        $token = $request->token;
//        $type = $request->type;
//        $cate = $request->cate;
//        $point = $request->point;
//        if (!$token) {
//            return $this->toJson(-2);
//        }
//        $uid = $this->accessGet($token);
//        if (!$uid) { return $this->toJson(-13); }
//        $user = new UserService();
//        $user->setPoint($user,$point);
//        return $this->toJson(0);
//    }

    /**
     * 获取省
     */
    public function getProviceList(Request $request)
    {
        $models = $this->user->getProvices();
        if (!count($models)) { return $this->toJson(-3); }
        return $this->toJson(0,$models);
    }

    /**
     * 获取市
     */
    public function getCityList(Request $request)
    {
        $pid = $request->pid;
        if (!$pid) { return $this->toJson(-2); }
        $models = $this->user->getCityByPid($pid);
        if (!count($models)) { return $this->toJson(-3); }
        return $this->toJson(0,$models);
    }

    /**
     * 获取区
     */
    public function getRegionList(Request $request)
    {
        $pid = $request->pid;
        if (!$pid) { return $this->toJson(-2); }
        $models = $this->user->getCityByPid($pid);
        if (!count($models)) { return $this->toJson(-3); }
        return $this->toJson(0,$models);
    }

    /**
     * 搜索内容初始化
     */
    public function setInit(Request $request)
    {
        $resData = $this->service->setInit();
        //清空搜索表
        SearchModel::truncate();
        SearchLogModel::truncate();
        $print = array();
        //公司
        foreach ($resData['coms'] as $keyCom => $com) {
            $search['type'] = 1;
            $search['from'] = $keyCom;
            $search['content'] = $com;
            $lastId = $this->service->add($search);
            $print[$lastId] = '公司'.$keyCom.'更新成功';
        }
        //工厂
        foreach ($resData['facs'] as $keyFac => $fac) {
            $search['type'] = 2;
            $search['from'] = $keyFac;
            $search['content'] = $fac;
            $lastId = $this->service->add($search);
            $print[$lastId] = '工厂'.$keyFac.'更新成功';
        }
        //工人
        foreach ($resData['staffs'] as $keyStaff => $staff) {
            $search['type'] = 3;
            $search['from'] = $keyStaff;
            $search['content'] = $staff;
            $lastId = $this->service->add($search);
            $print[$lastId] = '工人'.$keyStaff.'更新成功';
        }
        //货单
        foreach ($resData['bales'] as $keyBale => $bale) {
            $search['type'] = 4;
            $search['from'] = $keyBale;
            $search['content'] = $bale;
            $lastId = $this->service->add($search);
            $print[$lastId] = '货单'.$keyBale.'更新成功';
        }
        //岗位
        foreach ($resData['jobs'] as $keyJob => $job) {
            $search['type'] = 5;
            $search['from'] = $keyJob;
            $search['content'] = $job;
            $lastId = $this->service->add($search);
            $print[$lastId] = '岗位'.$keyJob.'更新成功';
        }
        dd($print);
    }
}